package com.blb.service.impl;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blb.entity.Role;
import com.blb.entity.RoleExample;
import com.blb.mapper.RoleMapper;
import com.blb.service.IRoleService;
import com.blb.utils.LayuiPage;
import com.mysql.jdbc.StringUtils;

@Service
public class RoleServiceImpl implements IRoleService {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public LayuiPage page(int page, int limit, String role) throws IOException {
		List<Role> list = roleMapper.page(page, limit, role);
		
		RoleExample roleExample = new RoleExample();
		if (!StringUtils.isNullOrEmpty(role)) roleExample.createCriteria().andRoleLike("%"+role+"%"); 
		int count = (int) roleMapper.countByExample(roleExample);
		return new LayuiPage(count, list);
	}

	@Override
	public String isExist(String role) throws IOException {
		RoleExample roleExample = new RoleExample();
		roleExample.createCriteria().andRoleEqualTo(role);
		
		List<Role> list = roleMapper.selectByExample(roleExample);
		if (!list.isEmpty()) {
			return "角色名已存在";
		}
		return "";
	}

}
